package autotd.netgui;
import autotd.*;
import autotd.server.*;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.*;


public class ButtonPanel extends JPanel implements ActionListener, AdvancedSimulatorListener {

	private static final int WIDTH = 70;
	private static final Font infoFont = new Font("LucidaSansRegular", 0, 11);
	private static final Font labelFont = new Font("LucidaSansRegular", Font.BOLD, 11);
	private static final Font buttonFont = new Font("LucidaSansRegular", 0, 11);
	private static final Font livesFont = new Font("LucidaSansRegular", Font.BOLD, 14);
	
	
	private SimulatorClient sim;
	
	private JLabel turns = new JLabel("Turn: ");
	private JLabel lives = new JLabel("Lives: ");
	private JLabel money = new JLabel("Money: ");
	private JTextArea infoLabels = new JTextArea(7, 8);
	private JTextArea infoBox = new JTextArea(7, 8);
	private JPanel buttons = new JPanel();
	
	//control buttons
	private JButton t = new JButton("Tick");
	private JButton t2 = new JButton("Tick x2");
	private JButton t4 = new JButton("Tick x4");
	private JButton t8 = new JButton("Tick x8");
	private JButton t16 = new JButton("Tick x16");
	private JButton t32 = new JButton("Tick x32");
	private JButton t64 = new JButton("Tick x64");
	private JButton t128 = new JButton("Tick x128");
	private JButton t256 = new JButton("Tick x256");
	private JButton t512 = new JButton("Tick x512");
	private JButton t1024 = new JButton("Tick x1024");
	
	private Vector<JButton> towerButtons;
	
	public ButtonPanel() {
		super();
		
		setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		
		c.gridx = 0;
		c.gridy = 0;
		c.ipady = 10;
		c.insets = new Insets(5, 10, 0, 10);
		c.anchor = GridBagConstraints.FIRST_LINE_START;
		turns.setFont(livesFont);
		add(turns, c);
		
		c.gridy = 1;
		lives.setFont(livesFont);
		add(lives, c);
		
		c.gridy = 2;
		money.setFont(livesFont);
		add(money, c);
		
		c.anchor = GridBagConstraints.PAGE_START;
		c.gridy = 3;
		add(buttons, c);
		
		c.anchor = GridBagConstraints.FIRST_LINE_START;
		c.gridy = 4;
		
		JPanel infoPanel = new JPanel(new GridLayout(1,2));
		add(infoPanel, c);
		infoPanel.add(infoLabels);
		infoPanel.add(infoBox);
		
		infoLabels.setFont(labelFont);
		infoLabels.setText("Name\r\nCost\r\nPower\r\nDelay\r\nRange\r\nSplash\r\nHeight\r\nElements");
		
		infoLabels.setEditable(false);
		infoLabels.setLineWrap(true);
		infoLabels.setMaximumSize(new Dimension(WIDTH, 300));
		infoLabels.setSize(new Dimension(WIDTH, 300));
		
		infoBox.setFont(infoFont);
		infoBox.setEditable(false);
		infoBox.setLineWrap(true);
		infoBox.setMaximumSize(new Dimension(WIDTH, 300));
		infoBox.setSize(new Dimension(WIDTH, 300));
		
		setInfoBoxVisible(false);
		
		//control buttons
		JPanel controlPanel = new JPanel();
		controlPanel.setLayout(new GridLayout(2,1));
		
		JPanel restOfButtons = new JPanel();
		restOfButtons.setLayout(new GridLayout(5,2));
		
		c.gridy = 5;
		c.anchor = GridBagConstraints.PAGE_START;
		c.weighty = 1.0;
		add(controlPanel, c);
		controlPanel.add(t);
		controlPanel.add(restOfButtons);
		restOfButtons.add(t2);
		restOfButtons.add(t4);
		restOfButtons.add(t8);
		restOfButtons.add(t16);
		restOfButtons.add(t32);
		restOfButtons.add(t64);
		restOfButtons.add(t128);
		restOfButtons.add(t256);
		restOfButtons.add(t512);
		restOfButtons.add(t1024);
		
		AL al = new AL();
		t.addActionListener(al);
		t2.addActionListener(al);
		t4.addActionListener(al);
		t8.addActionListener(al);
		t16.addActionListener(al);
		t32.addActionListener(al);
		t64.addActionListener(al);
		t128.addActionListener(al);
		t256.addActionListener(al);
		t512.addActionListener(al);
		t1024.addActionListener(al);
		
		init();
	}
	
	public void setSimulator(SimulatorClient sim) {
		this.sim = sim;
		sim.addAdvancedListener(this);
	}
	
	public void setInfoBoxVisible(boolean v) {
		infoBox.setVisible(v);
		infoLabels.setVisible(v);
	}
	
	public void init() {
		
		buttons.removeAll();
		towerButtons = new Vector<JButton>();
		
		//create tower buttons		 
		if(sim == null)
			return;
	
		buttons.setLayout(new GridLayout(2, sim.getTowerTypes().size()/2));
		
		for(TowerType type: sim.getTowerTypes()) {
			JButton tmp = new JButton(type.Name);
			tmp.setFont(buttonFont);
			tmp.addActionListener(this);
			towerButtons.add(tmp);
			buttons.add(tmp);
		}
	
		revalidate();		
	}

	public void setLives(int l) {
		lives.setText("Lives: " + l);
	}

	public void setMoney(int m) {
		money.setText("Money: $" + m);
	}
	
	public void setSim(SimulatorClient sim) {
		this.sim = sim;
	}
	
	public void setTurn(int turn){
		turns.setText("Turn: " + turn);
	}

	public void actionPerformed(ActionEvent e) {
		TowerType tt = sim.getTowerType(((JButton)e.getSource()).getText());
		infoBox.setText(tt.getInfo());
		setInfoBoxVisible(true);
		GUI.gui.setTowerType(tt);
	}
	
	//updates player data
	public void paint(Graphics g) {
		super.paint(g);
		
		if(sim != null) {
			setLives(sim.getPlayer().getLives());
			setMoney(sim.getPlayer().getMoney());
			setTurn(sim.getTurnNumber());
		}
	}
	
	private class AL implements ActionListener {
		public void actionPerformed(ActionEvent ae) {
			try{
			
			Object source = ae.getSource();
			       if (source== t ) {	GUI.gui.tick(1);
			} else if (source== t2) {	GUI.gui.tick(2);
			} else if (source== t4) {	GUI.gui.tick(4);
			} else if (source== t8) {	GUI.gui.tick(8);
			} else if (source== t16) {	GUI.gui.tick(16);
			} else if (source== t32) {	GUI.gui.tick(32);
			} else if (source== t64) {	GUI.gui.tick(64);
			} else if (source== t128) {	GUI.gui.tick(128);
			} else if (source== t256) {	GUI.gui.tick(256);
			} else if (source== t512) {	GUI.gui.tick(512);
			} else if (source== t1024) {	GUI.gui.tick(1024);
			}
			
			} catch(Exception e) {}	// catch any uncaught exceptions resulting from a user action
		}
	}
	
	public void monsterCreated(int mID, String type, int level, int x, int y) {}
	public void monsterMoved(int mID, int prevX, int prevY, int currX, int currY) {}
	public void monsterInjured(int mID, int health) {}
	public void monsterCompletedPath(int mID) {}
	public void towerPlaced(int tID, String type, int x, int y, int height) {}
	public void towerFired(int tID, int mID) {}
	public void playerChange(int lives, int money) {
		repaint();
	}
	public void turnChangedTo(int turnNum) {
		repaint();
	}
}
